package cn.chia.back.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import cn.chia.pojo.Orders;
import cn.chia.pojo.Ordertime;
import cn.chia.pojo.Peisongren;
import cn.chia.service.OrderService;
import cn.chia.service.OrdertimeService;
import cn.chia.service.SenderService;
import cn.chia.utils.BuildLogFile;
import cn.chia.utils.OnCustomerService;
import cn.chia.utils.Onsender;
/*
 * 于辉  后台配送员Action
 */
@Controller
public class SenderAction {
	private SenderService senderService;  //配送员Service
	private OrderService orderService;
	private OrdertimeService ordertimeService;
	private Peisongren peisongren;        //配送员对象
	private Orders order;
	private Set set;
	private int id;                       //配送员id
	private List<?> list;                 //配送员集合
	private String psrname;
	private String psrtel;
	private int state;
	
	public String login() throws Exception{
		System.out.println(psrname+"......"+psrtel);
		HttpServletRequest request = ServletActionContext.getRequest();
		peisongren = senderService.findByUserTel(psrname,psrtel);
		System.out.println(peisongren.toString());
		if(peisongren==null){
			return "error";
		}
		
		request.getSession().setAttribute("onsender",peisongren);
		//生成日志
		BuildLogFile.build(Onsender.sender().getPsrname()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"登陆本系统.\r\n");
		list = senderService.findOrderByStateUser("配送中");
		return "success";
	}
	
	/*
	 * 于辉  查询所有配送人
	 */
	public String list() throws Exception{
		list = senderService.findall();
		return "success";
	}
	
	/*
	 * 于辉  增加一个配送员
	 */
	public String add() throws Exception{
		peisongren.setFlag(1);
		senderService.insert(peisongren);
		BuildLogFile.build(OnCustomerService.customerService().getCsname()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"增加了一位配送员:"+peisongren.getPsrname()+"。\r\n");
		return "success";
	}
	
	/*
	 * 于辉  隐藏一个配送员
	 */
	public String delete() throws Exception{
		peisongren = senderService.findFrontById(id);
		peisongren.setFlag(0);
		senderService.delete(peisongren);
		BuildLogFile.build(OnCustomerService.customerService().getCsname()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"隐藏了一位配送员:"+peisongren.getPsrname()+"。\r\n");
		return "success";
	}
	
	/*
	 * 于辉  根据id查找配送员
	 */
	public String viewbyid() throws Exception{
		peisongren = senderService.findFrontById(id);
		return "success";
	}
	
	public String vieworderbyid() throws Exception{
		//某一条订单
		order = orderService.findById(id);
		//将这条订单的商品明细放到set集合中
		set = order.getOrderitemses();
		return "success";
	}
	/*
	 * 于辉  修改一个配送员
	 */
	public String update() throws Exception{
		senderService.update(peisongren);
		BuildLogFile.build(OnCustomerService.customerService().getCsname()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"修改了一位配送员:"+peisongren.getPsrname()+"。\r\n");
		return "success";
	}
	/*
	 * 于辉  更改订单状态为交易完成
	 */
	public String updatestate() throws Exception{
		order.setState("交易完成");
		Ordertime ot = ordertimeService.findById(order.getOrdertime().getOrdernumber());
		ot.setFinishtime(new Date());
		ordertimeService.update(ot);
		//表示此订单未被处理货处理完成
		orderService.updateFront(order);
		//生成日志
		BuildLogFile.build(Onsender.sender().getPsrname()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"将订单号为"+order.getOrderNumber()+"的订单状态改为交易完成。\r\n");
		return "success";
	}
	
	/*
	 * 于辉  查看配送中或交易完成状态的订单
	 */
	public String listordersbystate() throws Exception{
		if(state == 8){list = senderService.findOrderByStateUser("配送中");}
		if(state == 7){list = senderService.findOrderByStateUser("交易完成");}
		return "success";
	}
	/*
	 * 于辉  以下为各属性的get、set方法
	 */
	public Peisongren getPeisongren() {
		return peisongren;
	}
	public void setPeisongren(Peisongren peisongren) {
		this.peisongren = peisongren;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public List<?> getList() {
		return list;
	}
	public void setList(List<?> list) {
		this.list = list;
	}
	public String getPsrname() {
		return psrname;
	}

	public void setPsrname(String psrname) {
		this.psrname = psrname;
	}

	public String getPsrtel() {
		return psrtel;
	}

	public void setPsrtel(String psrtel) {
		this.psrtel = psrtel;
	}

	public Orders getOrder() {
		return order;
	}

	public void setOrder(Orders order) {
		this.order = order;
	}

	public Set getSet() {
		return set;
	}

	public void setSet(Set set) {
		this.set = set;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	@Resource(name="senderServiceImpl")
	public void setSenderService(SenderService senderService) {
		this.senderService = senderService;
	}
	@Resource(name="orderServiceImpl")
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	@Resource(name="ordertimeServiceImpl")
	public void setOrdertimeService(OrdertimeService ordertimeService) {
		this.ordertimeService = ordertimeService;
	}
}
